package com.yarolegovich.lovelydialog;

import ohos.agp.components.Button;
import ohos.agp.components.Checkbox;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.preferences.Preferences;
import ohos.utils.PacMap;

/**
 * Created by yarolegovich on 16.04.2016.
 */
public class LovelyInfoDialog extends AbsLovelyDialog<LovelyInfoDialog> {
    private static final String STORAGE = "ld_dont_show";
    private static final String KEY_DONT_SHOW_AGAIN = "key_dont_show_again";
    private Checkbox cbDontShowAgain;
    private Button confirmButton;
    private int infoDialogId;

    public LovelyInfoDialog(Context context) {
        super(context);
    }

    public LovelyInfoDialog(Context context, int theme) {
        super(context, theme);
    }

    {
        cbDontShowAgain = findView(ResourceTable.Id_cb_dont_show_again);
        confirmButton = findView(ResourceTable.Id_btn_confirm);
        confirmButton.setClickedListener(new ClickListenerDecorator(null, true));
        infoDialogId = -1;
        cbDontShowAgain.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (cbDontShowAgain.isChecked()) {
                    cbDontShowAgain.setButtonElement(ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_unselect));
                } else {
                    cbDontShowAgain.setButtonElement(ResUtil.getPixelMapDrawable(getContext(), ResourceTable.Media_select));
                }
            }
        });
    }

    public LovelyInfoDialog setNotShowAgainOptionEnabled(int dialogId) {
        infoDialogId = dialogId;
        cbDontShowAgain.setVisibility(Component.VISIBLE);
        confirmButton.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                boolean notShow = cbDontShowAgain.isChecked();
                storage(getContext()).putBoolean(String.valueOf(infoDialogId), notShow).flush();
                dismiss();
            }
        });
        return this;
    }

    public LovelyInfoDialog setNotShowAgainOptionChecked(boolean defaultChecked) {
        cbDontShowAgain.setChecked(defaultChecked);
        return this;
    }

    public LovelyInfoDialog setConfirmButtonText(int text) {
        return setConfirmButtonText(string(text));
    }

    public LovelyInfoDialog setConfirmButtonText(String text) {
        confirmButton.setText(text);
        return this;
    }

    public LovelyInfoDialog setConfirmButtonColor(int color) {
        confirmButton.setTextColor(new Color(color));
        return this;
    }

    @Override
    public CommonDialog show() {
        if (infoDialogId == -1) {
            return super.show();
        }

        boolean shouldShowDialog = !storage(getContext()).getBoolean(String.valueOf(infoDialogId), false);
        if (shouldShowDialog) {
            return super.show();
        } else {
            return super.create();
        }
    }

    @Override
    void onSaveInstanceState(PacMap outState) {
        super.onSaveInstanceState(outState);
        outState.putBooleanValue(KEY_DONT_SHOW_AGAIN, cbDontShowAgain.isChecked());
    }

    @Override
    void restoreState(PacMap savedState) {
        super.restoreState(savedState);
        cbDontShowAgain.setChecked(savedState.getBooleanValue(KEY_DONT_SHOW_AGAIN));
    }

    @Override
    protected int getLayout() {
        return ResourceTable.Layout_dialog_info;
    }

    public static void reset(Context context, int dialogId) {
        storage(context).putBoolean(String.valueOf(dialogId), false).flush();
    }

    private static Preferences storage(Context context) {
        return new DatabaseHelper(context).getPreferences(STORAGE);
    }
}
